/**
 * 
 */
package com.kelaite.procurement.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kelaite.procurement.dto.CompanyDTO;
import com.kelaite.procurement.dto.UnitDTO;
import com.kelaite.procurement.dto.UserDTO;
import com.kelaite.procurement.entity.TCity;
import com.kelaite.procurement.entity.TCompany;
import com.kelaite.procurement.entity.TMaterialMasterCentral;
import com.kelaite.procurement.entity.TMaterialMasterClient;
import com.kelaite.procurement.entity.TMaterialType;
import com.kelaite.procurement.entity.TProvince;
import com.kelaite.procurement.entity.TPurchaseOrganization;
import com.kelaite.procurement.entity.TUnit;
import com.kelaite.procurement.entity.TUser;
import com.kelaite.procurement.restful.BaseController;
import com.kelaite.procurement.service.CompanyService;
import com.kelaite.procurement.service.MaterialMasterCentralService;
import com.kelaite.procurement.service.MaterialMasterClientService;
import com.kelaite.procurement.service.MaterialTypeService;
import com.kelaite.procurement.service.ProvinceService;
import com.kelaite.procurement.service.PurchaseOrganizationService;
import com.kelaite.procurement.service.UnitService;
import com.kelaite.procurement.service.UserService;
import com.kelaite.procurement.utils.CurrentUserUtils;

/**
 * @author think
 *
 */
@Controller
@RequestMapping("/maintance/")
public class MaintanceController extends BaseController {
	
	@Autowired
	private MaterialMasterCentralService materialMasterCentralService;
	
	@Autowired
	private MaterialTypeService materialTypeService;
	
	@Autowired
	private UnitService unitService;
	
	@Autowired
	private PurchaseOrganizationService purchaseOrganizationService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private MaterialMasterClientService materialMasterClientService;
	
	@Autowired
	private ProvinceService provinceService;

	@RequestMapping(value = "/preCreateMaterial", method = {RequestMethod.GET, RequestMethod.POST})
	public String preCreateMaterial(Model model,HttpServletRequest request) {
		//TODO
		//对应物资编码在T_material_master_central
		List<TMaterialMasterCentral> tMaterialMasterCentralList = this.materialMasterCentralService.getAll();
		//物资类别在T_material_type
		List<TMaterialType> tMaterialTypeList = this.materialTypeService.getAll();
		//计量单位在T_unit
		List<UnitDTO> tUnitList = this.unitService.getAll();
		model.addAttribute("tMaterialMasterCentralList", tMaterialMasterCentralList);
		model.addAttribute("tMaterialTypeList", tMaterialTypeList);
		model.addAttribute("tUnitList", tUnitList);
		model.addAttribute("tMaterialMasterClient",new TMaterialMasterClient());
		Date now = new Date();
		long timeStart=now.getTime();
		model.addAttribute("suiji", timeStart);
		
		UserDTO userDTO = CurrentUserUtils.getInstance().getUser();
		TUser currentUser = this.userService.getObject(userDTO.getId());
		List<TMaterialMasterClient> tMaterialMasterClient =  this.materialMasterClientService.getByProperty("tCompany", currentUser.gettCompany());
		if(tMaterialMasterClient.size()>0){
			model.addAttribute("tMaterialMasterCentralList", tMaterialMasterClient.get(0));
			return "maintance/material_edit";
		}else{
			return "maintance/material_create";
		}
		
	}
	
	@RequestMapping(value = "/preCreateOrg", method = {RequestMethod.GET, RequestMethod.POST})
	public String preCreateOrganization(Model model,HttpServletRequest request) {
		//TODO
		//T_purchase_organization
		UserDTO userDTO = CurrentUserUtils.getInstance().getUser();
	    TUser currentUser = this.userService.getObject(userDTO.getId());
	    List<TCompany> tCompanyList = this.companyService.getByProperty("businessType", (short)1);
	    List<TProvince> province = this.provinceService.getAll();
	    
	    List<TPurchaseOrganization> tPurchaseOrganizationList = this.purchaseOrganizationService.getByProperty("tCompany",currentUser.gettCompany());
		model.addAttribute("tPurchaseOrganizationList", tPurchaseOrganizationList);
		model.addAttribute("tCompany", currentUser.gettCompany());
		model.addAttribute("purchaseOrganization",new TPurchaseOrganization());
		model.addAttribute("tCompanyList", tCompanyList);
		
		model.addAttribute("pList", province);
		return "maintance/purchase_org_create";
	}
	
	@RequestMapping(value = "/CreateOrganization", method = {RequestMethod.GET, RequestMethod.POST})
	public String CreateOrganization(@ModelAttribute("purchaseOrganization") TPurchaseOrganization purchaseOrganization,Model model,HttpServletRequest request) {
		
		int count=this.purchaseOrganizationService.getByOrganizationNo(purchaseOrganization.getOrganizationNo());
		if(count >0)
		{	
			request.setAttribute("errorMsg", "该组织编号已存在！");
			UserDTO userDTO = CurrentUserUtils.getInstance().getUser();
		    TUser currentUser = this.userService.getObject(userDTO.getId());
		    List<TPurchaseOrganization> tPurchaseOrganizationList = this.purchaseOrganizationService.getByProperty("tCompany",currentUser.gettCompany());
			model.addAttribute("tPurchaseOrganizationList", tPurchaseOrganizationList);
			model.addAttribute("tCompany", currentUser.gettCompany());
			model.addAttribute("purchaseOrganization",new TPurchaseOrganization());
			return "maintance/purchase_org_create";
		}
		
		String companyId=request.getParameter("companyId");
		TCompany company = new TCompany();
		company.setId(Long.parseLong(companyId));
		String userId=request.getParameter("userId");		
//		TUser user=new TUser();
//		user.setId(Long.parseLong(userId));
		TUser user=userService.getObject(Long.parseLong(userId));
		String provinceId=request.getParameter("provinceId");
		TProvince province=new TProvince();
		province.setId(Long.parseLong(provinceId));
		String cityId=request.getParameter("cityId");
		TCity city=new TCity();
		city.setId(Long.parseLong(cityId));
		
		purchaseOrganization.settCompany(company);
		purchaseOrganization.settUser(user);
		purchaseOrganization.settProvince(province);
		purchaseOrganization.settCity(city);
		
		this.purchaseOrganizationService.save(purchaseOrganization);
		
		request.removeAttribute("purchaseOrganization");
		return this.preCreateOrganization(model, request);
	}
	
	@RequestMapping(value = "/CreateMaterialClient", method = {RequestMethod.GET, RequestMethod.POST})
	public String CreateMaterialClient(@ModelAttribute("tMaterialMasterClient") TMaterialMasterClient tMaterialMasterClient,Model model,HttpServletRequest request) {
		
		String tUnitId=request.getParameter("tUnitId");
		TUnit tUnit=new TUnit();
		tUnit.setId(Long.parseLong(tUnitId));
		String materialTypeId=request.getParameter("materialTypeId");
		TMaterialType tMaterialType=new TMaterialType();
		tMaterialType.setId(Long.parseLong(materialTypeId));
		
		String materialMasterCentralId=request.getParameter("materialMasterCentralId");
		TMaterialMasterCentral tMaterialMasterCentral=new TMaterialMasterCentral();
		tMaterialMasterCentral.setId(Long.parseLong(materialMasterCentralId));
		
		tMaterialMasterClient.settUnit(tUnit);
		tMaterialMasterClient.settMaterialType(tMaterialType);
		tMaterialMasterClient.settMaterialMasterCentral(tMaterialMasterCentral);
		tMaterialMasterClient.setStatus(0);
		
		UserDTO userDTO = CurrentUserUtils.getInstance().getUser();
	    TUser currentUser = this.userService.getObject(userDTO.getId());
		
	    tMaterialMasterClient.settCompany(currentUser.gettCompany());
		this.materialMasterClientService.save(tMaterialMasterClient);
		
		return this.preCreateMaterial(model, request);
	}
	
	@Override
	protected Class<?> getCurrentClass() {
		return MaintanceController.class;
	}

}
